//
//  DMMessageModel.m
//  BaseAPP
//
//  Created by Evisu on 2017/8/8.
//  Copyright © 2017年 evisum. All rights reserved.
//

#import "DMMessageModel.h"
#import "EMTextMessageBody.h"
#import "EMImageMessageBody.h"

#define MAX_IMAGE_PIX 160

@implementation DMMessageModel

- (void)setMessage:(EMMessage *)message {

    _message = message;
    
    //消息方向
    if (message.direction == EMMessageDirectionSend) {
        _direction = MessageDirectionSend;
    } else {
        _direction = MessageDirectionReceive;
    }
    
    //消息类型
    switch (message.body.type) {
        case EMMessageBodyTypeText://文字消息
        {
            _type = MessageTypeText;
            
            EMTextMessageBody *textMessageBody = (EMTextMessageBody *)message.body;
            _text = textMessageBody.text;
        }
            break;
            
        case EMMessageBodyTypeImage://图片消息
        {
            _type = MessageTypeImage;
            
            EMImageMessageBody *imageMessageBody = (EMImageMessageBody *)message.body;
            UIImage *image = [UIImage imageWithContentsOfFile:imageMessageBody.thumbnailLocalPath];
//            _thumbnailSize = image.size;
            _thumbnailLocalPath = imageMessageBody.thumbnailLocalPath;
            _thumbnailRemotePath = imageMessageBody.thumbnailRemotePath;
            
            CGSize size = image.size;
            
            if (size.width > MAX_IMAGE_PIX || size.height > MAX_IMAGE_PIX) {
                
                CGFloat widhtRatio = MAX_IMAGE_PIX/size.width;
                CGFloat heightRatio = MAX_IMAGE_PIX/size.height;
                CGFloat scale = widhtRatio > heightRatio?heightRatio:widhtRatio;
                size = CGSizeMake(size.width*scale, size.height*scale);
            }
            _thumbnailSize = size;
            
        }
            break;
            
        default:
            break;
    }
    
}

@end
